package com.server.questionbank.snframe.mapper.Course;

import com.server.questionbank.snframe.controller.Course.pojo.AddExercises;
import com.server.questionbank.snframe.controller.Course.pojo.Exercises.UpdateExercises;
import com.server.questionbank.snframe.domain.Homework.TExercises;
import com.server.questionbank.snframe.domain.Course.TKnowledge;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TExercisesMapper {
    // 查询所有题目
    @Select("<script>" +
            "SELECT T_exercises.*, " +
            "CASE difficulty " +
            "WHEN 1 THEN '简单' " +
            "WHEN 2 THEN '中等' " +
            "WHEN 3 THEN '困难' " +
            "END AS difficulty_text " +
            "FROM T_exercises " +
            "<if test='knowledgeId != null'>, exec_knowledge </if> " +
            "WHERE 1=1 " +
            "<if test='exercisesId != null'> AND T_exercises.exercises_id = #{exercisesId} </if> " +
            "<if test='knowledgeId != null'> " +
                "AND exec_knowledge.exercises_id = T_exercises.exercises_id " +
                "AND exec_knowledge.record_id = #{knowledgeId} " +
            "</if> " +
            "<if test='courseId != null'> " +
                "AND course_id = #{courseId} " +
                "<if test='noChapter == true'> AND chapter_id IS NULL </if> " +
            "</if> " +
            "<if test='chapterId != null'> AND chapter_id = #{chapterId} </if> " +
            "<if test='dataType != null'> AND data_type = #{dataType} </if> " +
            "<if test='sortField != null and sortDirection != null'> " +
                "ORDER BY T_exercises.${sortField} ${sortDirection} " +
            "</if> " +
            "<if test='offset != null and pageSize != null'> LIMIT #{pageSize} OFFSET #{offset} </if> " +
            "</script>")
    List<TExercises> queryExercises(
            @Param("exercisesId") Long exercisesId, // 题目ID
            @Param("knowledgeId") Long knowledgeId, // 知识点ID
            @Param("courseId") Long courseId, // 课程ID
            @Param("noChapter") Boolean noChapter, // 是否查询无章节题目
            @Param("chapterId") Long chapterId, // 章节ID
            @Param("dataType") String dataType, // 数据类型
            @Param("sortField") String sortField, // 排序字段
            @Param("sortDirection") String sortDirection, // 排序方向
            @Param("offset") Long offset, // 偏移量
            @Param("pageSize") Long pageSize // 每页数量
    );

    // 查询所有题目数量
    @Select("<script>" +
            "SELECT COUNT(T_exercises.exercises_id) " +
            "FROM T_exercises " +
            "<if test='knowledgeId != null'>, exec_knowledge </if> " +
            "WHERE 1=1 " +
            "<if test='exercisesId != null'> AND T_exercises.exercises_id = #{exercisesId} </if> " +
            "<if test='knowledgeId != null'> " +
                "AND exec_knowledge.exercises_id = T_exercises.exercises_id " +
                "AND exec_knowledge.record_id = #{knowledgeId} " +
            "</if> " +
            "<if test='courseId != null'> " +
                "AND course_id = #{courseId} " +
                "<if test='noChapter == true'> AND chapter_id IS NULL </if> " +
            "</if> " +
            "<if test='chapterId != null'> AND chapter_id = #{chapterId} </if> " +
            "<if test='dataType != null'> AND data_type = #{dataType} </if> " +
            "</script>")
    Long queryCountExercises(
            @Param("exercisesId") Long exercisesId, // 题目ID
            @Param("knowledgeId") Long knowledgeId, // 知识点ID
            @Param("courseId") Long courseId, // 课程ID
            @Param("noChapter") Boolean noChapter, // 是否查询无章节题目
            @Param("chapterId") Long chapterId, // 章节ID
            @Param("dataType") String dataType // 数据类型
    );

    // 添加题目
    @Insert("<script>" +
            "INSERT INTO T_exercises (" +
            "   chapter_id, course_id, title, question_stem, exercise_type, difficulty, data_type, create_by, ele_remark " +
            ") VALUES (" +
            "   <if test='chapterId != null and chapterId > 0'>#{chapterId}</if> " +
            "   <if test='chapterId == null or chapterId &lt;= 0'>NULL</if>, " +
            "   <if test='courseId != null and courseId > 0'>#{courseId}</if>" +
            "   <if test='courseId == null or courseId &lt;= 0'>NULL</if>, " +
            "   #{title}, #{questionStem}, #{exerciseType}, #{difficulty}, #{dataType}, " +
            "   <if test='createBy != null and createBy > 0'>#{createBy}</if>" +
            "   <if test='createBy == null or createBy &lt;= 0'>NULL</if>, " +
            "   #{eleRemark} " +
            ")" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "exercisesId", keyColumn = "exercises_id")
    Long addExercises(AddExercises exercises);

    // 绑定知识点
    @Insert("<script>" +
            "INSERT INTO exec_knowledge (exercises_id, record_id) VALUES " +
            "<foreach collection='knowledges' item='knowledge' separator=','>" +
            "(#{exercisesId}, #{knowledge.recordId})" +
            "</foreach>" +
            "</script>")
    Long addKnowledge(@Param("exercisesId") Long exercisesId, @Param("knowledges") List<TKnowledge> knowledges);

    // 清空知识点
    @Delete("DELETE FROM exec_knowledge WHERE exercises_id = #{exercisesId}")
    Long clearKnowledge(@Param("exercisesId") Long exercisesId);

    @Update({
            "<script>",
            "UPDATE T_exercises",
            "<set>",
            "<if test='exercises.chapterId != null'>chapter_id = ",
            "<choose>",
            "<when test='exercises.chapterId == 0'>NULL,</when>",
            "<otherwise>#{exercises.chapterId},</otherwise>",
            "</choose>",
            "</if>",
            "<if test='exercises.courseId != null'>course_id = ",
            "<choose>",
            "<when test='exercises.courseId == 0'>NULL,</when>",
            "<otherwise>#{exercises.courseId},</otherwise>",
            "</choose>",
            "</if>",
            "<if test='exercises.title != null'>title = #{exercises.title},</if>",
            "<if test='exercises.questionStem != null'>question_stem = #{exercises.questionStem},</if>",
            "<if test='exercises.difficulty != null'>difficulty = #{exercises.difficulty},</if>",
            "<if test='exercises.updateBy != null'>update_by = #{exercises.updateBy},</if>",
            "<if test='exercises.eleRemark != null'>ele_remark = #{exercises.eleRemark},</if>",
            "</set>",
            "WHERE exercises_id = #{exercises.exercisesId}",
            "</script>"
    })
    Long updateExercises(@Param("exercises") UpdateExercises exercises);
//    @Update("<script>" +
//            "UPDATE T_exercises " +
//            "<set>" +
//            "<if test='chapterId != null and chapterId > 0'>chapter_id = #{chapterId}, </if> " +
//            "<if test='chapterId == null or chapterId &lt;= 0'>chapter_id = NULL, </if> " +
//            "<if test='courseId != null and courseId > 0'>course_id = #{courseId}, </if> "+
//            "</script>")
//    Long updateExercises(@Param("exercises") UpdateExercises exercises);

    // 修改知识点
    // 修改问题

}
